using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using eShop.Business;
using eShop.Utility;
using eShop.Model;
using System.Collections.Generic;
using eShop.Model.MetaData;

public partial class myaccount_edit_profile : SitePublicBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;
        if (!Page.IsPostBack)
        {
            if (SessionManager.IsUserLoggedIn)
            {
                LoadProfile();
            }
            else
            {
                Response.Redirect(CustomSiteMap.Login);
            }

           
        }
    }


    protected void LoadProfile()
    {
        try
        {
            
            User customerUser = SessionManager.LoggedUser;
            
            tbxPersonalFirstName.Text = customerUser.Profile.FirstName;
            tbxPersonalLastName.Text = customerUser.Profile.LastName;
            if (customerUser.Profile != null && customerUser.Profile.DateOfBirth.HasValue)
            {
                tbxPersonalDateOfBirth.Text = ECommerce.Library.ECommerce.Utility.FormatDateTime(customerUser.Profile.DateOfBirth.Value);
                
            }
                
            tbxPersonalEmailAddress.Text = customerUser.Profile.EmailAddress;
            rbtMale.Checked = customerUser.Profile.Gender.ToLower() == "male" ? true : false;
            rbtFemale.Checked = customerUser.Profile.Gender.ToLower() == "female" ? true : false;

            tbxContactPhoneNumber.Text = customerUser.Profile.PhoneNumber;
        }
        catch
        {
            uccResponseMessage.SetFailMessage("Failed to load your account information.");
            uccResponseMessage.Visible = true;
        }
    }

    protected void imbModify_Click(object sender, EventArgs e)
    {
        try
        {
           

            if (IsValid)
            {
                User customerUser = SessionManager.LoggedUser;
                
                if (customerUser != null)
                {
                    customerUser.Profile.FirstName = tbxPersonalFirstName.Text.Trim(); ;
                    customerUser.Profile.LastName = tbxPersonalLastName.Text.Trim(); ;
                    DateTime dateOfBirth;
                    if (!DateTime.TryParse(tbxPersonalDateOfBirth.Text, out dateOfBirth))
                    {
                        uccResponseMessage.SetFailMessage("Incorrect date.");
                        uccResponseMessage.Visible = true;
                        return;
                    }
                    customerUser.Profile.DateOfBirth = dateOfBirth;
                    

                    customerUser.Profile.EmailAddress = tbxPersonalEmailAddress.Text.Trim();

                    if (rbtMale.Checked)
                    {
                        customerUser.Profile.Gender = "Male";
                    }
                    else
                    {
                        customerUser.Profile.Gender = "Female";
                    }

                    


                    customerUser.Profile.PhoneNumber = tbxContactPhoneNumber.Text.Trim();



                    if (UserManager.UpdateCustomerUser(customerUser))
                    {
                       uccResponseMessage.SetSuccessMessage("Your account information updated.");
                        uccResponseMessage.Visible = true;
                    }
                    else
                    {
                        throw new Exception();
                    }
                }

            }
        }
        catch
        {
            uccResponseMessage.SetFailMessage("Failed to modify your account information.");
            uccResponseMessage.Visible = true;
        }
    }
}
